package abs.service;

import abs.dao.CompaniasDAO;
import abs.dao.DocumentosDAO;

import abs.domain.Companias;
import abs.domain.Documentos;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Documentos entities
 * 
 */

@Service("DocumentosService")
@Transactional
public class DocumentosServiceImpl implements DocumentosService {

	/**
	 * DAO injected by Spring that manages Companias entities
	 * 
	 */
	@Autowired
	private CompaniasDAO companiasDAO;

	/**
	 * DAO injected by Spring that manages Documentos entities
	 * 
	 */
	@Autowired
	private DocumentosDAO documentosDAO;

	/**
	 * Instantiates a new DocumentosServiceImpl.
	 *
	 */
	public DocumentosServiceImpl() {
	}

	/**
	 * Delete an existing Documentos entity
	 * 
	 */
	@Transactional
	public void deleteDocumentos(Documentos documentos) {
		documentosDAO.remove(documentos);
		documentosDAO.flush();
	}

	/**
	 * Save an existing Companias entity
	 * 
	 */
	@Transactional
	public Documentos saveDocumentosCompanias(String co030codi, Companias related_companias) {
		Documentos documentos = documentosDAO.findDocumentosByPrimaryKey(co030codi, -1, -1);
		Companias existingcompanias = companiasDAO.findCompaniasByPrimaryKey(related_companias.getCo000nocia());

		// copy into the existing record to preserve existing relationships
		if (existingcompanias != null) {
			existingcompanias.setCo000nocia(related_companias.getCo000nocia());
			existingcompanias.setCo000logo(related_companias.getCo000logo());
			existingcompanias.setCo000nomb(related_companias.getCo000nomb());
			existingcompanias.setCo000nosu(related_companias.getCo000nosu());
			existingcompanias.setCo000dire(related_companias.getCo000dire());
			existingcompanias.setCo000repr(related_companias.getCo000repr());
			existingcompanias.setCo000cont(related_companias.getCo000cont());
			existingcompanias.setCo000nreg(related_companias.getCo000nreg());
			existingcompanias.setCo000clca(related_companias.getCo000clca());
			existingcompanias.setCo000mofu(related_companias.getCo000mofu());
			existingcompanias.setCo000notr(related_companias.getCo000notr());
			existingcompanias.setCo000idtr(related_companias.getCo000idtr());
			existingcompanias.setCo000meci(related_companias.getCo000meci());
			existingcompanias.setCo000mepr(related_companias.getCo000mepr());
			existingcompanias.setCo000incc(related_companias.getCo000incc());
			existingcompanias.setCo000inut(related_companias.getCo000inut());
			existingcompanias.setCo000incp(related_companias.getCo000incp());
			existingcompanias.setCo000incf(related_companias.getCo000incf());
			existingcompanias.setCo000baca(related_companias.getCo000baca());
			existingcompanias.setCo000fir1(related_companias.getCo000fir1());
			existingcompanias.setCo000fir2(related_companias.getCo000fir2());
			existingcompanias.setCo000esta(related_companias.getCo000esta());
			existingcompanias.setCo000load(related_companias.getCo000load());
			existingcompanias.setCo000fead(related_companias.getCo000fead());
			existingcompanias.setCo000lomo(related_companias.getCo000lomo());
			existingcompanias.setCo000femo(related_companias.getCo000femo());
			related_companias = existingcompanias;
		}

		documentos.setCompanias(related_companias);
		related_companias.getDocumentoses().add(documentos);
		documentos = documentosDAO.store(documentos);
		documentosDAO.flush();

		related_companias = companiasDAO.store(related_companias);
		companiasDAO.flush();

		return documentos;
	}

	/**
	 * Load an existing Documentos entity
	 * 
	 */
	@Transactional
	public Set<Documentos> loadDocumentoss() {
		return documentosDAO.findAllDocumentoss();
	}

	/**
	 * Save an existing Documentos entity
	 * 
	 */
	@Transactional
	public void saveDocumentos(Documentos documentos) {
		Documentos existingDocumentos = documentosDAO.findDocumentosByPrimaryKey(documentos.getCo030codi());

		if (existingDocumentos != null) {
			if (existingDocumentos != documentos) {
				existingDocumentos.setCo030codi(documentos.getCo030codi());
				existingDocumentos.setCo030desc(documentos.getCo030desc());
				existingDocumentos.setCo030load(documentos.getCo030load());
				existingDocumentos.setCo030fead(documentos.getCo030fead());
				existingDocumentos.setCo030lomo(documentos.getCo030lomo());
				existingDocumentos.setCo030femo(documentos.getCo030femo());
			}
			documentos = documentosDAO.store(existingDocumentos);
		} else {
			documentos = documentosDAO.store(documentos);
		}
		documentosDAO.flush();
	}

	/**
	 * Return a count of all Documentos entity
	 * 
	 */
	@Transactional
	public Integer countDocumentoss() {
		return ((Long) documentosDAO.createQuerySingleResult("select count(o) from Documentos o").getSingleResult()).intValue();
	}

	/**
	 * Return all Documentos entity
	 * 
	 */
	@Transactional
	public List<Documentos> findAllDocumentoss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Documentos>(documentosDAO.findAllDocumentoss(startResult, maxRows));
	}

	/**
	 * Delete an existing Companias entity
	 * 
	 */
	@Transactional
	public Documentos deleteDocumentosCompanias(String documentos_co030codi, String related_companias_co000nocia) {
		Documentos documentos = documentosDAO.findDocumentosByPrimaryKey(documentos_co030codi, -1, -1);
		Companias related_companias = companiasDAO.findCompaniasByPrimaryKey(related_companias_co000nocia, -1, -1);

		documentos.setCompanias(null);
		related_companias.getDocumentoses().remove(documentos);
		documentos = documentosDAO.store(documentos);
		documentosDAO.flush();

		related_companias = companiasDAO.store(related_companias);
		companiasDAO.flush();

		companiasDAO.remove(related_companias);
		companiasDAO.flush();

		return documentos;
	}

	/**
	 */
	@Transactional
	public Documentos findDocumentosByPrimaryKey(String co030codi) {
		return documentosDAO.findDocumentosByPrimaryKey(co030codi);
	}
}
